<- Back to Spectre home page



Overview


This page is intended to provide a brief and high-level introduction to using R and Spectre. Additional educational material on using R and R Studio are available on many sites, including the RStudio education site or this R Spatial page.

Before getting started, see our getting started page for installation instructions for R and RStudio.



Working with R Studio


To interact with the R programming language, we recommend using RStudio.

Open RStudio

Open RStudio, and you should see something similar to the following:

  1. Top left = R script. This is a text editor where lines or segments of code can be ‘run’, which will send commands to R.
  2. Bottom left = console. When commands are sent to R, the console will show the progress/output/result. Code can also be entered and run directly in the console.
  3. Top right = workspace. Whenever you create an object in R (such as saving a set of data) it will show up here.
  4. Bottom right = various. This is mainly used for displaying plots (under ‘Plots’), investigating the packages (‘Packages’), or using the help section (‘Help’).


Create and save and R script

To get started, create a new .R file and save it

  • Make a folder on your desktop called “Spectre demo”
  • In RStudio, create a new R Script file (.R) called “MyScript” and save it in the folder you just created (Spectre demo).



Using R


R code basics

There are two important types of text commonly found in R scripts:

Comments Any line in R code that starts with a # is considered a comment. These are not executed by RStudio as R code, but rather are used as notes to the user.

This is a comment:

## Run the following line to find your current working directory

Executable code A line or segment of code can be run and will return some form of result. In the example below, the getwd() function will return the location of the current working directory.

This is the code:

getwd()

When the code is run, the output may look something like this:

[1] "/Users/Tom/Desktop"


Practice running code

For this demo we will use the ‘iris’ dataset, which consists of measurements of 150 flowers. Each row represents one flower, and each column represents a different measurement of that flower.

To run code in RStudio, we can either enter code into the script and selectively run elements of the code (preferred), or we can enter it directly into the console and run the code. For each of the code-blocks below, copy the code into your new script, press save, and then highlight and press CMD/CTRL return to execute the code

Read the dataset

Copy the following into your script, save, then highlight the code and press CMD/CTRL return. The first command we will run is to load the ‘iris’ dataset and save it as the object ‘dat’. The lines starting with ‘#’ are only comments, and will not excute as commands (even if you select them and press CMD + return).

## Part 1: read the dataset
     
# Use the 'iris' dataset (150 flowers one per row) with various measurement (each column is a different measurement)
dat <- iris

After executing, you should should see a new object in the workspace (top right). This will be called ‘dat’, containing 150 observations, and 5 variables.

~

Next we will review the dimensions of ‘dat’ (how many rows and columns) and preview data from the first 6 rows of dat.

Copy the following into your script, save, then highlight the code and press CMD/CTRL return.

# Determine the number of rows and columns in the dataset
dim(dat)
dat <- iris

You should now see the following in the console. Lines starting with ‘>’ denote the commands that were executed. Lines without ‘>’ are the output. As you can see below the request to show the dimensions of our dataset using dim(dat) has given us 150 rows and 5 columns.

[1] 150   5

Copy the following into your script, save, then highlight the code and press CMD/CTRL return.

# Examine the first few lines of dataset
head(dat)

You should now see the following in the console. Lines starting with ‘>’ denote the commands that were executed. Lines without ‘>’ are the output. The request to preview the first 6 rows of our data using head(dat) has shown us the contents of the first 6 rows.

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
Plot the dataset

Installing packages


Setting a working directory


R code basics




Using Spectre



Setup tutorial script and dataset


by a) clicking on the line or b) highlighting the text, and press CMD + return (Mac) or CTRL + Enter (Windows). You can run a single line of code (clicking on or highlighting the line), or multiple lines of code (highlight multiple lines of code).

If you highlight the code and press CMD + return (Mac) or CTRL + Enter (Windows), the code will be run. You can highlight multiple lines of code to run all at once.

How it works in RStudio

  1. Create an R script using RStudio (instructions below)
  2. Write code into the R file, or copy the code from our code blocks below. Make sure to SAVE.
  3. Click on a single line and press CMD + return (on Mac) or CTRL + enter (on Windows) to run the command on that line. Alternatively, you can highlight any section of text over one or more lines and press CMD + return (on Mac) or CTRL + enter (on Windows) to run the contents. Our scripts are designed so ‘modules’ of code can be run at a time.
  4. As above, nothing is returned if they are loaded successfully, or an error message is returned if they are not.